package com.woniuxy.rms.advice;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.woniuxy.rms.dao.LogsMapper;
import com.woniuxy.rms.pojo.Admins;
import com.woniuxy.rms.pojo.Logs;
import org.apache.shiro.SecurityUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Date;

/**
 * Auther: ChenGang  <br/>
 * Date: 2020/3/28 0028:下午 3:32  <br/>
 * Description:操作日志类
 */
@Component
@Aspect//切面
public class LogAdvice {
    @Autowired
    private LogsMapper logsMapper;

    @Before(value = "@annotation( com.woniuxy.rms.advice.LogPoint)")//指定连接点
    public void before(JoinPoint joinPoint){
        saveLog(getLog(joinPoint));//执行方法之前保存日志
    }


    /**
     * 保存日志信息
     * @param logs 日志
     */
    public void saveLog(Logs logs){
        logsMapper.insertSelective(logs);
    }

    /**
     * 获取日志信息
     * @param joinPoint 连接点
     * @return
     */
    public Logs getLog(JoinPoint joinPoint){
        Logs log = new Logs();//创建日志对象
        //获取request对象

        ServletRequestAttributes requestAttributes = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes());
        if (requestAttributes != null){
            HttpServletRequest request = requestAttributes.getRequest();
            //获取ip和uri  以及用户信息
            String ip = request.getRemoteHost();
            String uri = request.getRequestURI();
            Admins admin = (Admins) SecurityUtils.getSubject().getPrincipal();
            log.setIp(ip);
            log.setMethod(uri);
            log.setUserId(admin.getId());
            log.setRoleId(admin.getRoleId());
        }else {
            log.setUserId(1L);
        }
        //获取parm
        Object[] args = joinPoint.getArgs();
        //转成json串
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            log.setParams(objectMapper.writeValueAsString(args));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            log.setParams(null);
        }
        //执行的操作
        log.setOperation(getOperation(joinPoint));
        //创建时间
        log.setCreateTime(new Date());
        return log;
    }

    /**
     * 获取到操作类型   即注解的属性值
     * @param joinPoint
     * @return
     */
    public String getOperation(JoinPoint joinPoint){
        String name = joinPoint.getSignature().getName();//获取连接点方法名称
        //获取目标对象所属的类
        Class target = joinPoint.getTarget().getClass();
        //获取目标对象的方法
        Method[] methods = target.getMethods();
        //确定目标方法对象
        for (Method method : methods){
            if (method.getName().equals(name)){//方法名一致
                if (method.getParameterTypes().length == joinPoint.getArgs().length){//方法参数个数一致
                    String value = method.getAnnotation(LogPoint.class).value();
                    return value;
                }
            }
        }
        return null;
    }
}